
/*
 * Copyright (c) 2019. seven studio. @chengdu
 * seven studio in chengdu by lb
 * this project for snmp tool
 */

package com.xlx.openLink.controller;


import com.xlx.openLink.base.BaseDateMessage;
import com.xlx.openLink.base.BaseMessage;
import com.xlx.openLink.base.ThreadRunTimeExcption;
import com.xlx.openLink.entity.pojo.Conditions;
import com.xlx.openLink.enums.ResultCodeEnum;
import com.xlx.openLink.service.ConditionService;
import com.xlx.openLink.vo.RequestConditionList;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 *
 */

@Controller
@Slf4j
@RequestMapping("/con")
public class ConditionController {


    @Autowired
    private ConditionService conditionService;

    @ResponseBody
    @PostMapping("/add")
    public BaseMessage add(@RequestBody Conditions condition) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            conditionService.addCondition(condition);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("添加成功");
            log.info("添加成功-----");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
            log.error("添加添加条件错误{}", e.getMessage());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg("服务器错位");
            log.error("添加添加条件错误---");
        }
        return baseMessage;

    }


    @ResponseBody
    @GetMapping("/del")
    public BaseMessage del(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            conditionService.delCondition(pkGuid);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("添加成功");
            log.info("添加成功-----");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
            log.error("添加添加条件错误{}", e.getMessage());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg("服务器错位");
            log.error("添加添加条件错误---");
        }
        return baseMessage;
    }

    @ResponseBody
    @GetMapping("/select")
    public BaseMessage select(@RequestParam(value = "pkGuid") String pkGuid) {
        BaseDateMessage baseMessage = new BaseDateMessage();
        try {
            baseMessage.setData(conditionService.select(pkGuid));
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("成功");
            log.info("添加成功-----");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
            log.error("添加添加条件错误{}", e.getMessage());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg("服务器错位");
            log.error("失败---");
        }
        return baseMessage;
    }


    @ResponseBody
    @PostMapping("/update")
    public BaseMessage update(@RequestBody Conditions condition) {
        BaseMessage baseMessage = new BaseMessage();
        try {
            conditionService.updateCondition(condition);
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("操作成功");
            log.info("操作-----");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
            log.error("操作失败", e.getMessage());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg("服务器错误");
            log.error("服务器错误");
        }
        return baseMessage;
    }


    @ApiOperation(value = "分页查询", notes = "分页查询")
    @ApiImplicitParam(name = "requestConditionList", value = "分页请求参数", required = true, dataType = "RequestConditionList")
    @PostMapping("/listByPage")
    @ResponseBody
    public BaseDateMessage listByPage(@RequestBody RequestConditionList requestConditionList) {
        BaseDateMessage baseDateMessage = new BaseDateMessage();
        try {
            baseDateMessage.setData(conditionService.listByPage(requestConditionList));
            baseDateMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseDateMessage.setMsg("获取成功");
        } catch (ThreadRunTimeExcption excption) {
            baseDateMessage.buildSuccess(excption.getResultCodeEnum());
        } catch (Exception e) {
            baseDateMessage.buildFail(ResultCodeEnum.FAIL);
            baseDateMessage.setMsg("获取失败");
        }
        return baseDateMessage;
    }

    @ResponseBody
    @GetMapping("/selectGE")
    public BaseMessage selectGE() {
        BaseDateMessage baseMessage = new BaseDateMessage();
        try {
            baseMessage.setData(conditionService.selectGE());
            baseMessage.buildSuccess(ResultCodeEnum.SUCCESS);
            baseMessage.setMsg("成功");
            log.info("添加成功-----");
        } catch (ThreadRunTimeExcption e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg(e.getMessage());
            log.error("添加添加条件错误{}", e.getMessage());
        } catch (Exception e) {
            baseMessage.buildFail(ResultCodeEnum.FAIL);
            baseMessage.setMsg("服务器错位");
            log.error("失败---");
        }
        return baseMessage;
    }
}
